package com.yicong.ants.ui.coin;

import android.view.View;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.R;
import com.yicong.ants.bean.coin.CoinRecord;
import com.yicong.ants.databinding.Recycler2Binding;
import com.yicong.ants.databinding.RecyclerBinding;
import com.yicong.ants.manager.AdapterHelper;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 贡献值明细
 *
 * @author cch
 * @version 2020/7/15
 */
public class CoinRecordActivity extends BaseTitleBarActivity<Recycler2Binding> implements View.OnClickListener {
    StatefulBindQuickAdapter<CoinRecord> mAdapter;
    String mType = "0";
    private int empty = 0;

    @Override
    protected int getLayout() {
        return R.layout.recycler_2;
    }

    @Override
    protected void initEventAndData() {
        setTitleBarVisible(false);
        mDataBind.setClick(this);
        AppHelper.transStatus(getWindow());
        BarUtils.setStatusBarLightMode(this, false);

        String total = getIntent().getStringExtra("total");
        mDataBind.title.setText("门票明细");
        mDataBind.des.setText("门票余额");
        mDataBind.total.setText(total);

        // 筛选
        mDataBind.rightText.setText("明细筛选");
        mDataBind.rightImage.setImageResource(R.mipmap.icon_me_screen);
        mDataBind.right.setOnClickListener(view -> {
            String[] menuText = {"全部", "转赠好友明细", "收到转赠明细", "全球旅游补贴明细", "兑换任务明细", "每日贡献得"};
            String[] ids = {"0", "100", "110", "40", "130", "30"};
            Dialogs.showBottomItemDialog(mContext, "筛选展示明细记录", menuText, (dialog, which) -> {
                mType = ids[which];
                onLoadData();
            });
//            UiHelper.showItemsDialog(mContext, "筛选展示明细记录", menuText, (dialog, which) -> {
//                mType = ids[which];
//                onLoadData();
//            });
        });
        initAdapter();
        onLoadData();
        initTitleChange();
    }

    private void initTitleChange(){
        mDataBind.mScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            float alp = scrollY * 0.02f;
            if (alp < 0) {
                alp = 0;
            } else if (alp > 1) {
                alp = 1;
            }
            mDataBind.titleBgImage.setAlpha(alp);
        });
    }

    @Override
    protected void onLoadData() {
        loadPage(1);
    }

    private void initAdapter() {
        mDataBind.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new StatefulBindQuickAdapter<CoinRecord>(R.layout.coin_record_item, null) {
            @Override
            public void loadPageData(int page) {
                loadPage(page);
            }

            @Override
            protected void convert(DataBindViewHolder helper, CoinRecord item) {
                helper.getBinding().setVariable(BR.bean, item);
                View view = helper.getBinding().getRoot();
                if (helper.getLayoutPosition() == 0){
                    view.setPadding(view.getPaddingLeft() , SizeUtils.dp2px(2), view.getPaddingRight(),view.getPaddingBottom());
                }else{
                    view.setPadding(view.getPaddingLeft() , SizeUtils.dp2px(12), view.getPaddingRight(),view.getPaddingBottom());
                }
            }
        };
        mAdapter.bindToRecyclerView(mDataBind.recycler);
    }

    void loadPage(final int page) {
        final Map<String, String> params = new HashMap<>();
        params.put("page", String.valueOf(page));
        params.put("type", String.valueOf(mType));

        if (page == 1) {
            mAdapter.setViewState(MultiStateView.VIEW_STATE_LOADING);
        }
        addSubscribe(RetrofitHelper.getApis().coinRecord(params)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (page == 1){
                    mAdapter.getData().clear();
                    mAdapter.notifyDataSetChanged();
                }
                if (respBean.isCodeFail()) {
                    AdapterHelper.solveFailResp(mDataBind.recycler, page, respBean);
                    if (empty == 0){
                        empty = 1;
                        mAdapter.setEmptyView(R.layout.view_data_empty,mDataBind.recycler);
                    }
                    return;
                }
                List<CoinRecord> data = respBean.getList();
                mAdapter.solvePageData(data, page, respBean.getPageInfo().getCountPage());
                if (empty == 0){
                    empty = 1;
                    mAdapter.setEmptyView(R.layout.view_data_empty,mDataBind.recycler);
                }
            }, RxHelper.getSwitchableErrorConsumer(mAdapter)));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
